<template>
    <div>
        <h1>科室增加</h1>
        <table class="table table-primary">
            <tbody>
                <tr>
                    <th align="right"><span style="color: red;">*</span>科室名称</th>
                    <th><input type="text" v-model="queryinfo.DName"></th>
                </tr>
                <tr>
                    <th align="right"><span style="color: red;">*</span>科室图片</th>
                    <th>
                        <input type="file" @change="imgs">
                        <img :src="queryinfo.DPhoto" width="100" height="100" v-if="queryinfo.DPhoto!=''">
                    </th>
                </tr>
                <tr>
                    <th align="right"><span style="color: red;">*</span>成立时间</th>
                    <th><input type="date" v-model="queryinfo.CreateTime"></th>
                </tr>
                <tr>
                    <th align="right"></th>
                    <th><input type="button" value="增加" class="btn btn-primary" @click="ok"></th>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import { ref,reactive,onMounted } from 'vue';
import axios from 'axios';
import moment from 'moment';
import { useRoute,useRouter } from 'vue-router';
const route = useRoute()
const router = useRouter()

let queryinfo:any=reactive({
  "DName": "",
  "DPhoto": "",
  "CreateTime": "",
})

const imgs=(e:any)=>{
    var obj=e.target.files[0]
    let f = new FormData()
    f.append('file',obj)
    axios({
        url:'/api/Photo/Upload',
        method:'post',
        data:f
    })
    .then((res)=>{
        console.log(res);
        if(res.data=="必须为图片"){
            alert("必须为图片")
            return
        }
        if(res.data=="大小不能超过2M"){
            alert("大小不能超过2M")
            return
        }
        queryinfo.DPhoto=res.data
    })
}

const ok=()=>{
    if(queryinfo.DName==""){
        alert("科室名称不得为空")
        return
    }
    if(queryinfo.DPhoto==""){
        alert("科室图片不得为空")
        return
    }
    if(queryinfo.CreateTime==""){
        alert("成立时间不得为空")
        return
    }
    axios({
        url:'/api/Depart/AddDepart',
        method:'post',
        data:queryinfo
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("科室名称不能重复")
            return
        }
        if(res.data>0){
            alert("添加成功")
            router.push("/departmentlist")
        }
        else{
            alert("添加失败")
            return
        }
    })
}

</script>

<style scoped>

</style>